package com.swm.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

/**
 * @author 师文明
 * @data 2024/12/11 下午5:25
 **/
public class HtmlUtil {

    public static void main(String[] args) throws Exception {
        // 连接到新浪财经接口
        String url = "\n" +
                "https://q.10jqka.com.cn/index/index/board/all/field/zdf/order/desc/page/2/ajax/1/";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Content-Type", "text/html; charset=gbk");
        con.setRequestProperty("Cookie", "Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1733728111; HMACCOUNT=A6843E6FD6924CBF; Hm_lvt_722143063e4892925903024537075d0d=1733732270; Hm_lvt_929f8b362150b1f77b477230541dbbc2=1733732270; spversion=20130314; u_ukey=A10702B8689642C6BE607730E11E6E4A; u_uver=1.0.0; u_dpass=Zhy4ygECWWRkk5x%2FcZDR6Utj8UtGRIFZa50SBlGedA3o7wIgrA%2FGPercOX74OFgmHi80LrSsTFH9a%2B6rtRvqGg%3D%3D; u_did=A76F7DE5D73E4D49BA017DE9649B3F86; u_ttype=WEB; user_status=0; ttype=WEB; user=MDpteF9sM3VkcTR6NDQ6Ok5vbmU6NTAwOjc2MjA0MDc5Nzo3LDExMTExMTExMTExLDQwOzQ0LDExLDQwOzYsMSw0MDs1LDEsNDA7MSwxMDEsNDA7MiwxLDQwOzMsMSw0MDs1LDEsNDA7OCwwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMSw0MDsxMDIsMSw0MDoxNjo6Ojc1MjA0MDc5NzoxNzMzOTc5NDY3Ojo6MTczMTY1OTcwMDo2MDQ4MDA6MDoxNmEzNjc5Nzc1MTcwZDdmMjlmOTViNTY5MjhlOGU0MmE6ZGVmYXVsdF80OjA%3D; userid=752040797; u_name=mx_l3udq4z44; escapename=mx_l3udq4z44; ticket=19a834c99a2d8126c790a0d506be7a6c; utk=954de76f26995e414c6216528179ae29; historystock=301010%7C*%7C300403%7C*%7C002526%7C*%7C301076;Hm_lpvt_722143063e4892925903024537075d0d=1734055889; log=; Hm_lpvt_929f8b362150b1f77b477230541dbbc2=1734055889; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1734055889; " +
                "v=A8cl6XNZbAiNbeiJSDW968boVnCXzJLs9bAfJJm24pZ87ulmoZwr_gVwvCuq");

        Map<String, List<String>> headerFields = con.getHeaderFields();
        List<String> cookies = headerFields.get("Set-Cookie");

        // 从数据源获取实时股票数据
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), Charset.forName("GBK")));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

        in.close();
        String data = response.toString();
        System.out.println(data);

        // 解析HTML内容
        Document document = Jsoup.parse(data, "UTF-8");
        String text = document.text();
        System.out.println(text);
        Element emElement = document.select("tbody").first();
        Elements tr = emElement.select("tr");
        // System.out.println(tr);
        for (Element element : tr) {
            //System.out.println(element);
            Elements td = element.select("td");
           /* System.out.println(td.get(1).text());
            System.out.println(td.get(2).text());
            System.out.println(td.get(3).text());
            System.out.println(td.get(4).text());
            System.out.println(td.get(5).text());
            System.out.println(td.get(6).text());*/

            System.out.println("股票名称：" + td.get(2).text());
            System.out.println("当前价格：" + td.get(3).text());
            System.out.println("涨跌幅：" + td.get(4).text() + "%");
        }
        /* String totalCount = emElement.text();
        Elements allElements = emElement.getAllElements();
        for (Element allElement : allElements) {
            System.out.println(allElement);
            System.out.println("------------------------------------");

        }*/




        /*String[] split = data.split("<td><a href=\"http://stockpage.10jqka.com.cn");
        for (String s : split) {
            if(s.startsWith("/")){
                System.out.println(s);
            }
        }


        JSONObject jsonObject = JSONObject.parseObject(data);
        String string = jsonObject.get("data").toString();
        string=string.replace("[{","");
        string=string.replace("}]","");
        System.out.println(string);
        JSONObject jsonObject1 = JSONObject.parseObject(string);


        // 处理数据，提取股票信息
        String[] stockInfo = data.split(",");
        String stockName = stockInfo[0].split("=")[1];
        double price = Double.parseDouble(stockInfo[3]);
        double change = Double.parseDouble(stockInfo[4]);

        // 输出实时股票数据
        System.out.println("股票名称：" + stockName);
        System.out.println("当前价格：" + price);
        System.out.println("涨跌幅：" + change + "%");*/
    }
}
